/**
 * @(#) DExpenseMediator.as
 *
 * @date		2010. 12. 23.
 * @version		1.0
 * @author		Shin, Yunwoo
 * @description	석식비 - Mediator
 * 				 
 */
package com.skt.common_hr.dexpense.view
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.approval.RequestApprovalMediator;
	import com.skt.common_hr.common.components.CommonAlert;
	import com.skt.common_hr.common.puremvc.OverViewMediator;
	import com.skt.common_hr.dexpense.model.DExpenseListProxy;
	import com.skt.common_hr.dexpense.model.DExpenseProxy;
	import com.skt.common_hr.dexpense.view.components.DExpenseView;
	import com.skt.common_hr.dexpense.view.components.ReqDExpense;
	import com.wcl.utils.DateUtil;
	
	import flash.events.Event;
	
	import mx.core.IFlexDisplayObject;
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.multicore.interfaces.INotification;
	

	public class DExpenseMediator extends OverViewMediator
	{
		public static const NAME:String = "DExpenseMediator";
		
		private var bExecDialog:Boolean = false;
		private var begda:Date = new Date();
		private var endda:Date = new Date();
		
		/**
		* 생성자
		* 
		* @param
		* @return void
		*/
        public function DExpenseMediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
        }
 		
		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
 		override protected function initializeView():void
 		{
			super.initializeView(); 	
			setEventListener();			
 		}

 		private function setEventListener():void
 		{
			view.addEventListener(DExpenseView.CLICK_REQUEST, onClickRequest);
			view.addEventListener(DExpenseView.CLICK_QUERY, onClickQuery);
			view.addEventListener(DExpenseView.DELETE_REQUEST, onDeleteRequest);
			view.addEventListener(DExpenseView.MODIFY_REQUEST, onModifyRequest);
			view.addEventListener(DExpenseView.VIEW_REQUEST, onViewRequest);
 		}
 		

		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
 		override protected function activatedView():void
 		{
 			loadDinnerList();
 		}
 		
  		private function loadDinnerList():void
  		{
 			var proxy:DExpenseListProxy = facade.retrieveProxy(DExpenseListProxy.NAME) as DExpenseListProxy;
 			proxy.load(Variables.myPernr, view.begda, view.endda, view.curr_status);
  		}
  		
  		private function onClickQuery(e:Event):void
  		{
  			loadDinnerList();
  		}


 		private function onClickRequest(e:Event):void
  		{
  			execReqExpense();
  			facade.sendNotification(ReqDExpenseMediator.EXEC_NEW_REQUEST);
  		}
  		
 		private function onViewRequest(e:Event):void
  		{
  			var key:String = view.selected_vo.GWAREKEY;
  			execReqExpense();
  			//Alert.show("key = " + key);
  			facade.sendNotification(ReqDExpenseMediator.EXEC_READ_REQUEST, key);
    	}
  		

  		private var reqDExpense:IFlexDisplayObject = null;
  		private function execReqExpense():void
  		{
  			var reqMediator:ReqDExpenseMediator;
  			
  			if(reqDExpense==null) {	// 신규 생성
  				reqDExpense = PopUpManager.createPopUp(view, ReqDExpense, true);
  				PopUpManager.centerPopUp(reqDExpense);
				
				reqMediator = new ReqDExpenseMediator(reqDExpense);  	
				facade.registerMediator(reqMediator);			
  			}	
  			else {
  				PopUpManager.addPopUp(reqDExpense, view, true);
  				reqMediator = facade.retrieveMediator(ReqDExpenseMediator.NAME) as ReqDExpenseMediator;
  			}
  			bExecDialog = true;
  		}
 
		private function onModifyRequest(e:Event):void
  		{
  			var key:String = view.selected_vo.GWAREKEY;
  			execReqExpense();
  			facade.sendNotification(ReqDExpenseMediator.EXEC_MODI_REQUEST, key);
  		}

 		private function onDeleteRequest(e:Event):void
  		{
  			var message:String;
  			if(view.selected_vo.WSTATUS == "2") {
  				message = DateUtil.toDefaultString(view.selected_vo.BEGDA) + " 일자 석식비 신청을 취소하시겠습니까? ";
	  			CommonAlert.showMsgYesNo("", message, null, "확인", view, onOKDelete, onCancelDelete);
  			}
  			else if(view.selected_vo.WSTATUS == "4") {
  				message = "반려된 석식비 신청을 삭제하시겠습니까? ";
	  			CommonAlert.showMsgYesNo("", message, null, "확인", view, onOKDelete, onCancelDelete);
  			}
  		}
  		
  		private function onOKDelete():void
  		{
  			var proxy:DExpenseProxy = facade.retrieveProxy(DExpenseProxy.NAME) as DExpenseProxy;
  			proxy.del(view.selected_vo.GWAREKEY);
  		}
  		
  		private function onCancelDelete():void
  		{
  			
  		}
 		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array 
		{
			return basicInterestMessages.concat( [
					DExpenseListProxy.LOAD_DINNER_LIST_COMPLETE,
					DExpenseListProxy.LOAD_DINNER_LIST_FAILED,
					DExpenseProxy.DEL_DINNER_COMPLETE,
					DExpenseProxy.DEL_DINNER_FAILED,
					ReqDExpenseMediator.CLOSED_REQUEST_VIEW,
					RequestApprovalMediator.APPROVAL_COMPLETE
					]);
		}
		

		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			super.handleNotification(note);
			switch(note.getName()) 
			{
				case DExpenseListProxy.LOAD_DINNER_LIST_COMPLETE:
					view.dataProvider = (note.getBody() as DExpenseListProxy).dinnerList;
					view.total_days = (note.getBody() as DExpenseListProxy).daysOfConfirm;
					
					//Alert.show("loaded: " + view.dataProvider.length);
					//commonFacade.setStatusMessage("STATUS_LOADED_LIST", [view.dataProvider.length]);
					break;
				case DExpenseProxy.DEL_DINNER_COMPLETE:
					if(!bExecDialog) {
						loadDinnerList();
						CommonAlert.showMessage("", "삭제되었습니다.");
					}
					break;
				case RequestApprovalMediator.APPROVAL_COMPLETE:
					loadDinnerList();
					break;
				case ReqDExpenseMediator.CLOSED_REQUEST_VIEW:
					loadDinnerList();
					bExecDialog = false;
				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}
		
		
		
		
		
		
		
		
 
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event handler (from View, Internal or....)
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
        private function get view():DExpenseView
        {
            return viewComponent as DExpenseView;
        }
	}
}